#include "CustomBaudSingleton.h"
#include <qgridlayout.h>

CustomBaudSingleton& CustomBaudSingleton::getInstance()
{
	static CustomBaudSingleton instance;
	return instance;
}

std::shared_ptr<CustomBaudDialog> CustomBaudSingleton::getDialog()
{
	return dialog_;
}

CustomBaudSingleton::CustomBaudSingleton()
{
	dialog_ = std::make_shared<CustomBaudDialog>();
}





CustomBaudDialog::CustomBaudDialog()
{
	label = new QLabel(this);
	baud_edit = new QLineEdit(this);
	okButton = new QPushButton(this);
	cancelButton = new QPushButton(this);

	init_UI();
}

void CustomBaudDialog::init_UI()
{
	setWindowTitle("custom baud rate");

	QGridLayout* global_layout = new QGridLayout(this);
	label->setText("baud rate");
	label->setFixedWidth(100);
	okButton->setText("ok");
	cancelButton->setText("cancel");


	global_layout->addWidget(label, 0, 0);
	global_layout->addWidget(baud_edit, 0, 1);
	global_layout->addWidget(okButton, 1, 0);
	global_layout->addWidget(cancelButton, 1, 1);

	this->setLayout(global_layout);

	connect(okButton, &QPushButton::clicked, this, [=]() {
		int baud_value = this->baud_edit->text().toInt();
		emit accepted(baud_value);
		accept();
		});
	connect(cancelButton, &QPushButton::clicked, this, [=]() {
		emit rejected();
		reject();
		});
}
